﻿/* $RCSFile: PortalCrmProgressModuleControl.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/WebControls/PortalCrmProgressModuleControl.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:30  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.Web.UI;
using AddonNice.Helpers;

namespace AddonNice.UI.WebControls
{
    public class CancelProgressException : Exception
    {
        public CancelProgressException(): base()
        {}
        public CancelProgressException(string msg): base(msg)
        {}
        public CancelProgressException(string msg,Exception ex): base(msg,ex)
        {}
    }

    public class PortalCrmProgressModuleControl : PortalCRMModuleControl
    {

        #region progress Management

        RadProgressArea _ModuleProgressArea   =   null;
        public RadProgressArea ModuleProgressArea
        {
            get
            {
                if (_ModuleProgressArea == null)
                    _ModuleProgressArea = (RadProgressArea)FindControl("RadProgressArea1");
                return _ModuleProgressArea;
            }
            set
            {
                _ModuleProgressArea = value;
            }
        }
    
    
        protected int ProgressPrimaryTotal
        {
            get
            {
                object ob = ViewState["ProgressPrimaryTotal"];
                if (ob != null)
                    return (int)ob;
                return 0;
            }
            set
            {
                ViewState["ProgressPrimaryTotal"] =   value;
            }
        }

        /// <summary>
        /// current Primary position in progress
        /// </summary>
        public int ProgressPrimCount            =   0;
        
        /// <summary>
        /// One step for each script and each module to install
        /// </summary>
        public int ProgressTotalSteps              
        {
            get
            {
                object ob = ViewState["ProgressTotalSteps"];
                if (ob != null)
                    return (int)ob;
                return 0;
            }
            set
            {
                ViewState["ProgressTotalSteps"] =   value;
            }
        }

        /// <summary>
        /// current secondary position in progress
        /// </summary>
        public int ProgressSecCount             =   0;
        
        #endregion progress Management

        #region Install progress management

        /*
            PrimaryTotal The maximum value of the primary progress indicators. By default, RadProgressArea displays the request size. 
            PrimaryValue The current value of the primary progress indicators. By default, RadProgressArea displays the uploaded bytes count. 
            PrimaryPercent The current value of the primary progress, expressed as a percentage of PrimaryTotal. 
            SecondaryTotal The maximum value of the secondary progress indicators. By default, RadProgressArea displays the total number of files. 
            SecondaryValue The current value of the secondary progress indicators. By default, RadProgressArea displays the number of files already uploaded. 
            SecondaryPercent The current value of the secondary progress, expressed as a percentage of SecondaryTotal. 
            CurrentOperationText The description of the current operation. By default, RadProgressArea displays the name of the currently uploading file. 
            TimeEstimated The estimated time until the operation completes. 
            TimeElapsed The time that has elapsed from the beginning of the operation. 
            Speed The speed of the process. By default, RadProgressArea displays the upload speed.
         */

        /// <summary>
        /// Call it in Load event on each call to avoid surprises if progress area is inside an hideden control on firts call
        /// </summary>
        public void InitProgress()
        {
            ModuleProgressArea.Localization.UploadedFiles     =   LocalizeHelper.Localize("PROGRESS_COMPLETED_STEPS","Completed Steps: ");
            ModuleProgressArea.Localization.CurrentFileName   =   LocalizeHelper.Localize("PROGRESS_STEP","Step: ");
            ModuleProgressArea.Localization.TotalFiles        =   LocalizeHelper.Localize("PROGRESS_TOTAL_STEPS","Total Steps: ");
        }

        /// <summary>
        /// Call it on process start after having fixed the value for ProgressPrimaryTotal and ProgressTotalSteps
        /// </summary>
        public void StartProgress()
        {
            RadProgressContext context      =   RadProgressContext.Current;
            if (ProgressPrimaryTotal != 0)
            {
                context.PrimaryTotal        =   ProgressPrimaryTotal;
                context.PrimaryValue        =   "0";
                context.PrimaryPercent      =   "0";
            }
            context.SecondaryTotal          =   ProgressTotalSteps;
            context.SecondaryValue          =   "0";
            context.SecondaryPercent        =   "0";
            context.CurrentOperationText    =   "Start";
        }

        /// <summary>
        /// Call it when primary count change to reset secondary total
        /// </summary>
        public void ResetProgressSecTotal(int tot)
        {
            RadProgressContext context      =   RadProgressContext.Current;
            ProgressTotalSteps              =   tot;
            context.SecondaryTotal          =   tot;
            ProgressSecCount                =   0;
            context.SecondaryValue          =   "0";
            context.SecondaryPercent        =   "0";
        }

        /// <summary>
        /// Call it when process is finished (use a finally block!)
        /// </summary>
        public void EndProgress()
        {
            RadProgressContext.Current.OperationComplete = true;
        }

        /// <summary>
        /// Call it each time secondary count increments or when text changes
        /// </summary>
        public void SetUpdateProgress(string text,int primVal,int secVal)
        {
            RadProgressContext progress = RadProgressContext.Current;
            if ( primVal != 0 && (ProgressPrimaryTotal != 0))
            {
                progress.PrimaryValue       =   primVal.ToString();
                progress.PrimaryPercent     =   (primVal*100/ProgressPrimaryTotal).ToString();
            }
            if ( ProgressTotalSteps != 0 )
            {
                progress.SecondaryValue     =   secVal.ToString();
                progress.SecondaryPercent   =   (secVal*100/ProgressTotalSteps).ToString();
            }

            progress.CurrentOperationText   =   text;

            //Cancel button was clicked or the browser was closed, so stop processing
            if (!Response.IsClientConnected)
            {
                throw new CancelProgressException();
            };
        }

        #endregion Install progress management
    
    }
}
